// created by noellynch
// Jun 8, 2009

package com.sceneworker.app;

import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JFrame;

import com.acarter.scenemonitor.SceneMonitor;
import com.jme.renderer.Camera;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.app.globals.cacheobjects.RenderToTextureCacheObject;
import com.sceneworker.app.globals.cacheobjects.RootNodeCacheObject;
import com.sceneworker.app.input.SceneWorkerInputHandler;

/**
 * SceneWorkerAppHandler provides the following;<br />
 * <br/>
 * <ul>
 * <li>Update input handler for node selections/transformations</li>
 * <li>Update all registered objects in SceneMonitor</li>
 * <li>Update all TextureRenderer scenes</li>
 * <li>Render any selections</li>
 * <li>Render SceneMonitor</li>
 * <li>Render all TextureRenderers</li>
 * <li>Render light "sphere" if a light is selected</li>
 * <li>Render any triangle that is selected</li>
 * </ul>
 * @author noellynch
 *
 */
public class StandardJMEController implements ISceneWorkerToolsController {

	private		SceneWorkerInputHandler			mcl_ipHandler;
    private		StandardJMEToolsPalette		mcl_toolsPalette;
    private		StandardJMERenderer				mcl_render;
    
    public	StandardJMEController() {
    	
    }
    
    public	SceneWorkerInputHandler		getInputHandler() {
        return mcl_ipHandler;
    }

    public	StandardJMEToolsPalette		getToolsPalette() {
        return mcl_toolsPalette;
    }
    
    public	StandardJMERenderer		getRenderer() {
    	return mcl_render;
    }
    
    private	void	initialiseToolsPalette() {
    	// initialise tools palette
        mcl_toolsPalette = new StandardJMEToolsPalette(this);
        JDialog		lcl_paletteFrame = new JDialog();
        lcl_paletteFrame.setTitle("Tools");
        lcl_paletteFrame.add(mcl_toolsPalette);
        lcl_paletteFrame.pack();
		lcl_paletteFrame.setResizable(false);
		lcl_paletteFrame.setVisible(true);
    }
    
    /* (non-Javadoc)
	 * @see com.sceneworker.app.ISceneWorkerToolsController#initialise(java.lang.Object)
	 */
    public	void	initialise(Object cl_node) {
    	SceneWorkerGlobals.inst().addToCache(new RootNodeCacheObject((Spatial)cl_node));
        
        // initialise the scene worker input handler
        mcl_ipHandler = new SceneWorkerInputHandler(this);
        mcl_ipHandler.initialiseInputHandler();

        initialiseToolsPalette();
		
        // create the renderer
        mcl_render = new StandardJMERenderer(this);
    }
    
    /* (non-Javadoc)
	 * @see com.sceneworker.app.ISceneWorkerToolsController#render(float)
	 */
    public	void	render(float f_tpf) {
    	// call the renderer
    	mcl_render.render(f_tpf);
    }    

    /* (non-Javadoc)
	 * @see com.sceneworker.app.ISceneWorkerToolsController#update(float)
	 */
    public	void	update(float f_timePerFrame) {
    	// update the input handler
        mcl_ipHandler.updateInputHandler((Node)SceneWorkerGlobals.inst().getFromCache(RootNodeCacheObject.ROOT_NODE).getValue());

        // update the monitor panel
        SceneMonitor.getMonitor().updateViewer(f_timePerFrame);

        // update debug info
        Camera		lcl_cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();
        String	lstr_cam = String.format("cam location %.2f %.2f %.2f", lcl_cam.getLocation().x, lcl_cam.getLocation().y, lcl_cam.getLocation().z);
        mcl_render.getDebugText().print(lstr_cam);

        // update all render to texture scenes
        updateRTTScenes(f_timePerFrame);
    }

    /**
     * Update all render to texture scenes.
     * @param f_tpf
     */
    protected	void	updateRTTScenes(float f_tpf) {
        ArrayList<?>		lcla_rtt = SceneWorkerGlobals.inst().getAllInstancesInCache(RenderToTextureCacheObject.class);
        for(Object lcl_rtt : lcla_rtt) {
            RenderToTextureCacheObject		lcl_rttObj = (RenderToTextureCacheObject)lcl_rtt;
            if(lcl_rttObj.mcl_scenes != null) {
                lcl_rttObj.mcl_scenes.updateGeometricState(f_tpf, true);
            }
        }
    }

    
    
}
